package com.chis.ms;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Tj {


    public static void main(String[] args) throws Exception {
        String mainpath = "C:\\Users\\wlj\\Desktop\\wlj\\2015";
        File file = new File(mainpath);
        File[] files = file.listFiles();

        Set<String> dhlist = new HashSet<>();

        for (File file2 : files) {
            File[] zipfiles = file2.listFiles();
            for (File file3 : zipfiles) {

                String zippath = file3.getAbsolutePath();
                if(!zippath.endsWith("zip")){
                    continue;
                }
                String xmlpath = file3.getAbsolutePath().replace(file3.getName(),"data");
                try {
                    unzip(zippath,xmlpath);
                } catch (Exception e){
                    System.out.println(zippath);
                    continue;
                }

                String filepath = xmlpath+"\\data.xml";
                File datafile = new File(filepath);
                BufferedReader reader = null;
                try {
                    reader= new BufferedReader(new FileReader(datafile));
                } catch (Exception e){
                    System.out.println(zippath);
                    continue;
                }


                while (true) {
                    String nextline = null;
                    try {
                        nextline = reader.readLine();
                        if (nextline != null && nextline.length() > 8) {
                            if(nextline.contains("FHDBH")){
                                String dh = nextline.substring(nextline.indexOf(">")+1,nextline.indexOf("</"));
                                dhlist.add(dh);
                            }



                        }


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (nextline == null) break;
                }


            }
        }

        for(String s : dhlist){
            System.out.println(s);
        }
    }

    public static void unzip(String zipFilePath, String desDirectory) throws Exception {

        File desDir = new File(desDirectory);
        if (!desDir.exists()) {
            boolean mkdirSuccess = desDir.mkdir();
            if (!mkdirSuccess) {
                throw new Exception("创建解压目标文件夹失败");
            }
        }
        // 读入流
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath));
        // 遍历每一个文件
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            if (zipEntry.isDirectory()) { // 文件夹
                String unzipFilePath = desDirectory + File.separator + zipEntry.getName();
                // 直接创建
                mkdir(new File(unzipFilePath));
            } else { // 文件
                String unzipFilePath = desDirectory + File.separator + zipEntry.getName();
                File file = new File(unzipFilePath);
                // 创建父目录
                mkdir(file.getParentFile());
                // 写出文件流
                BufferedOutputStream bufferedOutputStream =
                        new BufferedOutputStream(new FileOutputStream(unzipFilePath));
                byte[] bytes = new byte[1024];
                int readLen;
                while ((readLen = zipInputStream.read(bytes)) != -1) {
                    bufferedOutputStream.write(bytes, 0, readLen);
                }
                bufferedOutputStream.close();
            }
            zipInputStream.closeEntry();
            zipEntry = zipInputStream.getNextEntry();
        }
        zipInputStream.close();
    }

    private static void mkdir(File file) {
        if (null == file || file.exists()) {
            return;
        }
        mkdir(file.getParentFile());
        file.mkdir();
    }


}
